package guoanmaker.operator.business.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import guoanmaker.operator.business.model.OperatorFeedback;
import guoanmaker.operator.business.model.PersonalUser;
import guoanmaker.operator.business.modelnobind.OperatorFeedbackMove;
import guoanmaker.operator.business.repository.OperatorFeedbackRepository;

@Service
public class OperatorFeedbackService implements OperatorFeedbackServiceInterface {

	@Autowired
	private OperatorFeedbackRepository operatorFeedbackRepository;
	
	@Autowired
	private OperatorSensitiveWordServiceInterface peratorSensitiveWordService;
	
	@PersistenceContext
	private EntityManager manager;
	
	public Map<String,Object> insertOperatorFeedback(String userId, String feedbackContext) {
		Map<String,Object> map=new HashMap<String,Object>();
		Set<String> set=peratorSensitiveWordService.sensitiveWordVerification(feedbackContext);
		if(set!=null&&set.size()>0){
			map.put("stauts", -1);
			map.put("set", set);
			return map;
		}
		PersonalUser personalUser=new PersonalUser();
		personalUser.setId(userId);
		OperatorFeedback operatorFeedback=new OperatorFeedback();
		operatorFeedback.setPersonalUser(personalUser);
		operatorFeedback.setFeedbackContext(feedbackContext);
		operatorFeedback.setFeedbackDatetime(new Date());
		operatorFeedback=operatorFeedbackRepository.save(operatorFeedback);
		map.put("stauts", 1);
		map.put("operatorFeedback", operatorFeedback);
		return map;
	}
	@Override
	public Map<String, Object> selectOperatorFeedbackPage(Integer page, Integer size) {
		Map<String, Object> map=new HashMap<String, Object>();
		String  jpql="select new guoanmaker.operator.business.modelnobind.OperatorFeedbackMove(u.username,o.feedbackDatetime,"
				+ "o.feedbackContext) from OperatorFeedback o left join o.personalUser u order by o.feedbackDatetime desc";
		Query query=manager.createQuery(jpql);
		int count=query.getResultList().size();
		query.setMaxResults(size).setFirstResult(page*size);
		List<OperatorFeedbackMove> list=query.getResultList();
		map.put("count", count);
		map.put("list", list);
		return map;
	}

	
}
